/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.pf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.pf.BaseWelfareExpendDTO;
import com.plian.system.entity.pf.BaseWelfareExpend;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.IBaseWelfareExpendService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.BaseWelfareExpendVO;
import com.plian.system.wrapper.pf.BaseWelfareExpendWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 职工福利费支出明细主表 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/basewelfareexpend")
public class BaseWelfareExpendController extends BaseController {

	private IBaseWelfareExpendService baseWelfareExpendService;

	private FormWorkflowService formWorkflowService;

	private BaseWelfareExpendWrapper baseWelfareExpendWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	public R<BaseWelfareExpendVO> detail(@RequestBody BaseWelfareExpend baseWelfareExpend) {
		BaseWelfareExpend detail = baseWelfareExpendService.getOne(Condition.getQueryWrapper(baseWelfareExpend));
		BaseWelfareExpendVO baseWelfareExpendVO = baseWelfareExpendWrapper.entityToVO(detail);
		if (baseWelfareExpendVO.getType() == 1){
			baseWelfareExpendVO.setFillingUnitName(baseWelfareExpendVO.getFillingUnitName() + "（单户）");
		}else {
			baseWelfareExpendVO.setFillingUnitName(baseWelfareExpendVO.getFillingUnitName() + "（合并）");
		}
		return baseWelfareExpendService.getDetailObj(baseWelfareExpendVO);
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public ListResult<BaseWelfareExpendVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("filling_year");
		}
		QueryWrapper<BaseWelfareExpend> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new BaseWelfareExpend()), entityList
		);
		TokenSearch<BaseWelfareExpend> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<BaseWelfareExpend> pages = baseWelfareExpendService.page(Condition.getPage(query), queryWrapper);
		ListResult<BaseWelfareExpendVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(baseWelfareExpendWrapper.entityToVO(pages.getRecords()));
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("职工福利费支出明细-提交")
	public R submit(@Valid @RequestBody BaseWelfareExpend baseWelfareExpend) {
		String checkMessage = baseWelfareExpendService.checkMonth(baseWelfareExpend);
		if (StringUtil.isNotBlank(checkMessage)){
			return R.fail(checkMessage);
		}
		boolean result = baseWelfareExpendService.submit(baseWelfareExpend);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(baseWelfareExpend.getId()),baseWelfareExpend.getFormType(),processVariable);
		return R.status(result);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("职工福利费支出明细-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return R.status(baseWelfareExpendService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
	}

	/**
	 * 职工福利费支出明细主表填报树
	 * @param baseWelfareExpend
	 * @return
	 */
	@PostMapping("/tree")
	public ObjResult tree(@RequestBody BaseWelfareExpend baseWelfareExpend){
		return baseWelfareExpendService.tree(baseWelfareExpend);
	}

//	/** TODO DEL
//	 * 根据填报单位id、填报年月 获取合计数据
//	 * @param baseWelfareExpend
//	 * @return
//	 */
//	@PostMapping("/getSummaryObj")
//	public ObjResult getSummaryObj(@RequestBody BaseWelfareExpend baseWelfareExpend){
//		return baseWelfareExpendService.getSummaryObj(baseWelfareExpend);
//	}

    /**
     * 树列表 分页
     */
    @PostMapping("/treeList")
    public ListResult<PageData> treeList(@RequestBody Query query) {
       return baseWelfareExpendService.treeList(query);
    }

	/**
	 * 职工福利费支出明细主表填报树 新增
	 * @param baseWelfareExpend
	 * @return
	 */
	@PostMapping("/add/tree")
	@Resubmit(delaySeconds = 0)
	@LogOperation("职工福利费支出明细-填报树新增")
	public ObjResult addTree(@RequestBody BaseWelfareExpend baseWelfareExpend){
		return baseWelfareExpendService.addTree(baseWelfareExpend);
	}

	/**
	 * 获取下级已填报数据
	 * @param baseWelfareExpend
	 * @return
	 */
	@PostMapping("/childList")
	public ObjResult childList(@RequestBody BaseWelfareExpend baseWelfareExpend){
        return baseWelfareExpendService.childList(baseWelfareExpend);
	}

	/**
	 * 根据主表id 获取合计数据
	 * @param baseWelfareExpend
	 * @return
	 */
	@PostMapping("/childSummary")
	public ObjResult childSummary(@RequestBody BaseWelfareExpend baseWelfareExpend){
		return baseWelfareExpendService.childSummary(baseWelfareExpend);
	}

	/**
	 * 合计数据来源
	 * @param baseWelfareExpendDTO
	 * @return
	 */
	@PostMapping("/summaryDetail")
	public ObjResult summaryDetail(@RequestBody BaseWelfareExpendDTO baseWelfareExpendDTO){
		return baseWelfareExpendService.summaryDetail(baseWelfareExpendDTO);
	}

	/**
	 * 员工福利excel导出
	 * @param
	 * @return
	 */
	@PostMapping(value = "/exportExcel", produces = "application/json;charset=UTF-8")
	public void exportExcel(@RequestBody BaseWelfareExpendVO baseWelfareExpendVO, HttpServletResponse response){
		baseWelfareExpendService.exportExcel(baseWelfareExpendVO, response);
	}

}
